<template>
  <t-dialog
    :header="header"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    width="40%"
    top="5%"
  >
   <div>
     <t-form  ref="form" label-align="top" :data="formData" :rules="rules">
       <t-form-item  name="remark" label="短信内容：">
         <t-textarea  :autosize="{minRows: 8}"
                      v-model="formData.remark"  placeholder="请输入内容"/>
       </t-form-item>
     </t-form>
   </div>
    <template #footer>
      <t-button theme="default" @click="closeDialog">取消</t-button>
      <t-button theme="primary" @click="onSubmit()">提交</t-button>
    </template>
  </t-dialog>
</template>

<script>
export default {
  name: "send-msg",
  props: {
    objId: [String,Number],
    header: {
      type: String,
      default : '',
    }
  },
  data(){
    return {
      visible: true,
      formData:{
        id: this.objId, // 用户Id
        remark: ""
      },
      rules: {
        remark: [
          {required: true, message: "短信内容不能为空"}
        ],
      },
    }
  },
  methods:{
    closeDialog() {
      this.$emit("update:visible", false);
    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          const confirmDia = this.$dialog.confirm({
            header: "提示",
            body: `是否确认提交？`,
            theme: "warning",
            onConfirm: ({e}) => {
              confirmDia.update({
                confirmBtn: {
                  loading: true,
                },
              })
              this.$api.system.user.sendMsg(this.formData).then(response => {
                this.$message.success('提交成功');
                this.$emit("reload");
                this.$emit("update:visible", false);
                confirmDia.destroy();
              }).catch((e) => {
                confirmDia.update({
                  confirmBtn: {
                    loading: false,
                  },
                })
                this.$message.error(e.toString());
              });
            },
          })

        }else {
          const firstError = Object.values(validateResult)[0]?.[0]?.message;
          this.$message.error(firstError);
        }
      })
    },
  }
}
</script>

<style scoped>

</style>
